package com.yvon.maple.service.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.google.common.collect.Lists;
import com.yvon.boot.mybatis.BaseServiceImpl;
import com.yvon.maple.service.system.mapper.RolePermissionMapper;
import com.yvon.maple.service.system.dto.RolePermissionDTO;
import com.yvon.maple.service.system.entity.RolePermission;
import com.yvon.maple.service.system.service.PermissionService;
import com.yvon.maple.service.system.service.RolePermissionService;
import com.yvon.maple.service.system.vo.RoleResourceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author : Yvon
 * @since : 2021-09-27
 */
@Service
public class RolePermissionServiceImpl extends BaseServiceImpl<RolePermission, RolePermissionMapper> implements RolePermissionService {

    @Autowired
    private PermissionService permissionService;

    @Override
    public boolean save(Long roleId, Collection<Long> permissionIds) {
        if (Objects.isNull(permissionIds)) {
            return true;
        }
        return saveRolePermission(roleId, permissionIds);
    }

    @Override
    public boolean update(Long roleId, Collection<Long> permissionIds) {
        if (permissionIds == null) {
            return true;
        }
        if (permissionIds.size() == 0) {
            removeByRoleIds(Collections.singletonList(roleId));
        }
        return saveRolePermission(roleId, permissionIds);
    }

    @Override
    public List<RolePermissionDTO> getByRoleId(Long roleId) {
        return baseMapper.getByRoleIds(Lists.newArrayList(roleId));
    }

    @Override
    public Map<Long, List<RolePermissionDTO>> getByRoleIds(Collection<Long> roleIds) {
        if (CollUtil.isNotEmpty(roleIds)) {
            List<RolePermissionDTO> groupings = baseMapper.getByRoleIds(roleIds);
            return groupings.stream().collect(Collectors.groupingBy(RolePermissionDTO::getRoleId));
        }
        return new HashMap<>(1);
    }

    @Override
    public boolean removeByRoleIds(Collection<Long> roleIds) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roleIds);
        return SqlHelper.retBool(baseMapper.delete(queryWrapper));
    }

    @Override
    public boolean removeByPermissionIds(Collection<Long> permissionIds) {
        QueryWrapper<RolePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("permission_id", permissionIds);
        return SqlHelper.retBool(baseMapper.delete(queryWrapper));
    }

    @Override
    public List<RoleResourceVo> getRoleResource() {
        return getBaseMapper().getRoleResource();
    }

    @Override
    public List<String> getPermsByUserId(Long userId) {
        return getBaseMapper().getPermsByUserId(userId);
    }

    private boolean saveRolePermission(Long roleId, Collection<Long> permissionIds) {
        List<RolePermission> list = permissionIds.stream().map(permissionId -> {
            RolePermission rolePermission = new RolePermission();
            rolePermission.setRoleId(roleId);
            rolePermission.setPermissionId(permissionId);
            return rolePermission;
        }).collect(Collectors.toList());
        return saveBatch(list);
    }
}
